package com.other;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

/**
 * Created by baiguantao on 2017/9/6.
 */
public class SendEmail {
    private MimeMessage mimeMsg;
    private Session session;
    private Properties props;
    private String username;
    private String password;
    private Multipart mp;
    private static String emailserve="1107530849@qq.com";
    private static String emailservepwd="itnxtgsetebxggce";//pop3smtp
    //private String emailservepwd="vmvpvgueazgqgeed";//三方授权
   // private String emailservepwd="xxyqibypjowxjjhf";//exchange

    public   static void   SendMails(String text,String Subject,String mail) throws Exception {
        Properties props = new Properties();

        // 开启debug调试
        props.setProperty("mail.debug", "true");
        // 发送服务器需要身份验证
        props.setProperty("mail.smtp.auth", "true");
        // 设置邮件服务器主机名
         String smtpHostName = "smtp." + mail.split("@")[1];
        props.setProperty("mail.host", smtpHostName);
        // 发送邮件协议名称
        props.setProperty("mail.transport.protocol", "smtp");

        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);

        Session session = Session.getInstance(props);

        Message msg = new MimeMessage(session);
        msg.setSubject(Subject);
        msg.setText(text);
        msg.setFrom(new InternetAddress(emailserve));

        Transport transport = session.getTransport();
        transport.connect("smtp.qq.com", emailserve, emailservepwd);

        transport.sendMessage(msg, new Address[] { new InternetAddress(mail) });
        transport.close();
    }


    public static String generateWordpwd() {
//      String[] beforeShuffle = new String[] { "2", "3", "4", "5", "6", "7",
//              "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
//              "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
//              "W", "X", "Y", "Z" };
        String[] beforeShuffle = new String[] { "1","2", "3", "4", "5", "6", "7", "8", "9"};
        List list = Arrays.asList(beforeShuffle);
        Collections.shuffle(list);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
        }
        String afterShuffle = sb.toString();
        String result = "DJ@"+afterShuffle.substring(2, 9);
        return result;
    }
    public static void main(String[] args)  throws Exception {
        SendEmail.SendMails("我是内容","标题","baiguantao@126.com");

    }

    }
